package com.ds.common.log;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;

import java.util.Arrays;

/**
 * @author WeiShaoying
 */

public class ToggleableLogger implements Logger {
    public static final ToggleableLogger log = new ToggleableLogger();
    private final Logger logger;

    public ToggleableLogger() {
        this.logger = LoggerFactory.getLogger(getClass());
    }

    @Override
    public String getName() {
        return logger.getName();
    }

    @Override
    public boolean isTraceEnabled() {
        return logger.isTraceEnabled();
    }

    @Override
    public void trace(String s) {
        logMessage("trace", s);
    }

    @Override
    public void trace(String s, Object o) {
        logMessage("trace", s, o);
    }

    @Override
    public void trace(String s, Object o, Object o1) {
        logMessage("trace", s, o, o1);
    }

    @Override
    public void trace(String s, Object... objects) {
        logMessage("trace", s, objects);
    }

    @Override
    public void trace(String s, Throwable throwable) {
        logMessage("trace", s, throwable);
    }

    @Override
    public boolean isTraceEnabled(Marker marker) {
        return logger.isTraceEnabled(marker);
    }

    @Override
    public void trace(Marker marker, String s) {
        logMessage(marker, "trace", s);
    }

    @Override
    public void trace(Marker marker, String s, Object o) {
        logMessage(marker, "trace", s, o);
    }

    @Override
    public void trace(Marker marker, String s, Object o, Object o1) {
        logMessage(marker, "trace", s, o, o1);
    }

    @Override
    public void trace(Marker marker, String s, Object... objects) {
        logMessage(marker, "trace", s, objects);
    }

    @Override
    public void trace(Marker marker, String s, Throwable throwable) {
        logMessage(marker, "trace", s, throwable);
    }

    @Override
    public boolean isDebugEnabled() {
        return logger.isDebugEnabled();
    }

    @Override
    public void debug(String s) {
        logMessage("debug", s);
    }

    @Override
    public void debug(String s, Object o) {
        logMessage("debug", s, o);
    }

    @Override
    public void debug(String s, Object o, Object o1) {
        logMessage("debug", s, o, o1);
    }

    @Override
    public void debug(String s, Object... objects) {
        logMessage("debug", s, objects);
    }

    @Override
    public void debug(String s, Throwable throwable) {
        logMessage("debug", s, throwable);
    }

    @Override
    public boolean isDebugEnabled(Marker marker) {
        return logger.isDebugEnabled(marker);
    }

    @Override
    public void debug(Marker marker, String s) {
        logMessage(marker, "debug", s);
    }

    @Override
    public void debug(Marker marker, String s, Object o) {
        logMessage(marker, "debug", s, o);
    }

    @Override
    public void debug(Marker marker, String s, Object o, Object o1) {
        logMessage(marker, "debug", s, o, o1);
    }

    @Override
    public void debug(Marker marker, String s, Object... objects) {
        logMessage(marker, "debug", s, objects);
    }

    @Override
    public void debug(Marker marker, String s, Throwable throwable) {
        logMessage(marker, "debug", s, throwable);
    }

    @Override
    public boolean isInfoEnabled() {
        return logger.isInfoEnabled();
    }

    @Override
    public void info(String s) {
        logMessage("info", s);
    }

    @Override
    public void info(String s, Object o) {
        logMessage("info", s, o);
    }

    @Override
    public void info(String s, Object o, Object o1) {
        logMessage("info", s, o, o1);
    }

    @Override
    public void info(String s, Object... objects) {
        logMessage("info", s, objects);
    }

    @Override
    public void info(String s, Throwable throwable) {
        logMessage("info", s, throwable);
    }

    @Override
    public boolean isInfoEnabled(Marker marker) {
        return logger.isInfoEnabled(marker);
    }

    @Override
    public void info(Marker marker, String s) {
        logMessage(marker, "info", s);
    }

    @Override
    public void info(Marker marker, String s, Object o) {
        logMessage(marker, "info", s, o);
    }

    @Override
    public void info(Marker marker, String s, Object o, Object o1) {
        logMessage(marker, "info", s, o, o1);
    }

    @Override
    public void info(Marker marker, String s, Object... objects) {
        logMessage(marker, "info", s, objects);
    }

    @Override
    public void info(Marker marker, String s, Throwable throwable) {
        logMessage(marker, "info", s, throwable);
    }

    @Override
    public boolean isWarnEnabled() {
        return logger.isWarnEnabled();
    }

    @Override
    public void warn(String s) {
        logMessage("warn", s);
    }

    @Override
    public void warn(String s, Object o) {
        logMessage("warn", s, o);
    }

    @Override
    public void warn(String s, Object... objects) {
        logMessage("warn", s, objects);
    }

    @Override
    public void warn(String s, Object o, Object o1) {
        logMessage("warn", s, o, o1);
    }

    @Override
    public void warn(String s, Throwable throwable) {
        logMessage("warn", s, throwable);
    }

    @Override
    public boolean isWarnEnabled(Marker marker) {
        return logger.isWarnEnabled(marker);
    }

    @Override
    public void warn(Marker marker, String s) {
        logMessage(marker, "warn", s);
    }

    @Override
    public void warn(Marker marker, String s, Object o) {
        logMessage(marker, "warn", s, o);
    }

    @Override
    public void warn(Marker marker, String s, Object o, Object o1) {
        logMessage(marker, "warn", s, o, o1);
    }

    @Override
    public void warn(Marker marker, String s, Object... objects) {
        logMessage(marker, "warn", s, objects);
    }

    @Override
    public void warn(Marker marker, String s, Throwable throwable) {
        logMessage(marker, "warn", s, throwable);
    }

    @Override
    public boolean isErrorEnabled() {
        return logger.isErrorEnabled();
    }

    @Override
    public void error(String s) {
        logMessage("error", s);
    }

    @Override
    public void error(String s, Object o) {
        logMessage("error", s, o);
    }

    @Override
    public void error(String s, Object o, Object o1) {
        logMessage("error", s, o, o1);
    }

    @Override
    public void error(String s, Object... objects) {
        logMessage("error", s, objects);
    }

    @Override
    public void error(String s, Throwable throwable) {
        logMessage("error", s, throwable);
    }

    @Override
    public boolean isErrorEnabled(Marker marker) {
        return logger.isErrorEnabled(marker);
    }

    @Override
    public void error(Marker marker, String s) {
        logMessage(marker, "error", s);
    }

    @Override
    public void error(Marker marker, String s, Object o) {
        logMessage(marker, "error", s, o);
    }

    @Override
    public void error(Marker marker, String s, Object o, Object o1) {
        logMessage(marker, "error", s, o, o1);
    }

    @Override
    public void error(Marker marker, String s, Object... objects) {
        logMessage(marker, "error", s, objects);
    }

    @Override
    public void error(Marker marker, String s, Throwable throwable) {
        logMessage(marker, "error", s, throwable);
    }

    private void logMessage(String level, String message, Object... params) {
        if (LogSwitch.getConfig().openLogWriter) {
            StackTraceElement element = Thread.currentThread().getStackTrace()[3];
            String formattedMessage = String.format("%s.%s (%s:%d) - %s", element.getClassName(), level, element.getFileName(), element.getLineNumber(), message);
            String finalMessage = String.format("%s - %s", formattedMessage, String.join(" ", Arrays.stream(params).map(String::valueOf).toArray(String[]::new)));
            String logOutput = String.format("%s - %s", level.toUpperCase(), finalMessage);
            logByLevel(level, logOutput);
        }
    }

    private void logMessage(Marker marker, String level, String message, Object... params) {
        if (LogSwitch.getConfig().openLogWriter) {
            StackTraceElement element = Thread.currentThread().getStackTrace()[3];
            String formattedMessage = String.format("%s.%s (%s:%d) - %s", element.getClassName(), level, element.getFileName(), element.getLineNumber(), message);
            String finalMessage = String.format("%s - %s", formattedMessage, String.join(" ", Arrays.stream(params).map(String::valueOf).toArray(String[]::new)));
            String logOutput = String.format("%s - %s", level.toUpperCase(), finalMessage);
            logByLevel(marker, level, logOutput);
        }
    }


    private void logByLevel(String level, String message) {
        switch (level) {
            case "trace":
                this.logger.trace(message);
                break;
            case "debug":
                this.logger.debug(message);
                break;
            case "info":
                this.logger.info(message);
                break;
            case "warn":
                this.logger.warn(message);
                break;
            case "error":
                this.logger.error(message);
        }
    }

    private void logByLevel(Marker marker, String level, String message) {
        switch (level) {
            case "trace":
                this.logger.trace(marker, message);
                break;
            case "debug":
                this.logger.debug(marker, message);
                break;
            case "info":
                this.logger.info(marker, message);
                break;
            case "warn":
                this.logger.warn(marker, message);
                break;
            case "error":
                this.logger.error(marker, message);
        }
    }
}
